﻿using System;
using System.Web.Script.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
//
namespace PerceptiveMCAPI.Export.Types
{
	public class listOutput : Api_BaseOutput
	{
		public listResults result { get; set; }
		public listParms inputParms { get; private set; }
		// --------------------------------------------------------
		public listOutput( listInput input )
			: base( input )
		{
			this.inputParms = input.parms;
			result = new listResults();
		}
		//
		public override string ToString()
		{
			return string.Format( "Records returned: {0}", result.data.Count );
		}
		//
		#region  ===================================================================== format result
		internal void Format( string api_Request, List<string> api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			//
			this.result = FormatResults( api_Response );
		}
		// ----------------------------------------------------------------- FormResults
		private listResults FormatResults( List<string> exportResult )
		{
			listResults results = new listResults();
			JavaScriptSerializer jss = new JavaScriptSerializer();
			results.header = jss.Deserialize<List<string>>( exportResult[0] );
			//
			for ( int ix = 1; ix < exportResult.Count; ix++ )
			{
				List<object> fields = jss.Deserialize<List<object>>( exportResult[ix] );
				results.data.Add( fields );
			}
			return results;
		}

		#endregion
	}
}
